package com.cloudbroker.bcs.csvc.lightinvesting.facade.service;

import com.alibaba.fastjson.JSON;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.csvc.lightinvesting.api.service.LedThreeService;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.DataDictionaryQueryForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.base.HeaderForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.baseRelevanted.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.cashTransactions.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.AdviserteamAddributionQryForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.HisBusinessQryForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.StockBlackTabForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.interestRelated.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.DataDictionaryBackVO;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.IFS.UserLoginBack;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.TradLoginBackVo;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.TradLoginVo;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.baseRelevanted.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.cashTransactions.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.fansRelated.AdviserteamAddributionQryBackVo;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.fansRelated.HisBusinessQryBackVo;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.fansRelated.StockBlackTabBackVo;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.interestRelated.*;
import com.cloudbroker.bcs.csvc.lightinvesting.facade.service.aspect.UserCache;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.base.vo.BrokerVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import util.MD5Util;
import util.RSAEncrypt;
import util.UserTokenUtil;

import javax.annotation.Resource;

import static com.cloudbroker.bcs.csvc.lightinvesting.api.constants.LedT2FunctionIds.*;

/**
 * Created by fangyf18371 on 2016/12/15.
 * 领投宝三阶段
 */
@Service
public class LedThreeServiceFacade implements LedThreeService{
    private static final Logger log = LoggerFactory.getLogger(LedThreeServiceFacade.class);

    @Resource
    private LedT2RemoteService ledT2RemoteService;

    /**
     * 投顾当日剩余允许打赏金额查询
     * @param param
     * @return
     */
    @Override
    public AllowExcepByDayVo func_extcomacct_rewark_amount_qry(AllowExcepByDayForm param) {
        return ledT2RemoteService.callT2(EXTCOMACCT_REWARK_AMOUNT_QRY,param,AllowExcepByDayVo.class);
    }

    /**
     * 达人改签申请
     * @param param
     * @return
     */
    @Override
    public CmuserSignChangeVo func_comuser_sign_change(CmuserSignChangeForm param) {
        return ledT2RemoteService.callT2(COMUSER_SIGN_CHANGE,param,CmuserSignChangeVo.class);
    }

    /**
     * 达人改签申请审核不通过
     * @param param
     * @return
     */
    @Override
    public SigncChangeNopassVo func_comuser_sign_change_verify_nopass(SigncChangeNopassForm param) {
        return ledT2RemoteService.callT2(COMUSER_SIGN_CHANGE_VERIFY_NOPASS,param,SigncChangeNopassVo.class);
    }

    /**
     * 达人改签申请审核通过
     * @param param
     * @return
     */
    @Override
    public SigncChangePassVo func_comuser_sign_change_verify_pass(SigncChangePassForm param) {
        return ledT2RemoteService.callT2(COMUSER_SIGN_CHANGE_VERIFY_PASS,param,SigncChangePassVo.class);
    }

    /**
     * 达人签约记录查询
     * @param param
     * @return
     */
    @Override
    public ComuserSignRecordQryBackVo func_comuser_sign_record_qry(ComuserSignRecordQryForm param) {
        return ledT2RemoteService.callT2(COMUSER_SIGN_RECORD_QRY,param,ComuserSignRecordQryBackVo.class);
    }

    /**
     * 历史证券成交查询
     * @param param
     * @return
     */
    @Override
    public HisBusinessQryBackVo func_his_business_qry(HisBusinessQryForm param) {
        return ledT2RemoteService.callT2(HIS_BUSINESS_QRY,param,HisBusinessQryBackVo.class);
    }

    /**
     * 投顾信息修改
     * @param param
     * @return
     */
    @Override
    public ComacctAdviserModVo func_comacct_adviser_mod(ComacctAdviserModForm param) {
        return ledT2RemoteService.callT2(COMACCT_ADVISER_MOD,param,ComacctAdviserModVo.class);
    }

    /**
     * 银行转账历史查询
     * @param param
     * @return
     */
    @Override
    public TransferHisLogQryBackVo func_extcomtrade_his_bktransfer_qry(TransferHisLogQryForm param) {
        return ledT2RemoteService.callT2(EXTCOMTRADE_HIS_BKTRANSFER_QRY,param,TransferHisLogQryBackVo.class);
    }

    /**
     * 订约申请查询
     * @param param
     * @return
     */
    @Override
    public SignfocusApplyQryBackVo func_extcompay_signfocus_apply_qry(SignfocusApplyQryForm param) {
        return ledT2RemoteService.callT2(EXTCOMPAY_SIGNFOCUS_APPLY_QRY,param,SignfocusApplyQryBackVo.class);
    }

    /**
     * 当日单客户允许打赏单投顾剩余金额查询
     * @param param
     * @return
     */
    @Override
    public LimitedAmountQryVo func_extcompay_comdate_limited_amount_qry(LimitedAmountQryForm param) {
        return ledT2RemoteService.callT2(EXTCOMPAY_COMDATE_LIMITED_AMOUNT_QRY,param,LimitedAmountQryVo.class);
    }

    /**
     * 投顾团队归属信息查询
     * @param param
     * @return
     */
    @Override
    public AdviserteamAddributionQryBackVo func_comadviser_adviserteam_qry(AdviserteamAddributionQryForm param) {
        return ledT2RemoteService.callT2(COMADVISER_ADVISERTEAM_QRY,param,AdviserteamAddributionQryBackVo.class);
    }

    /**
     * 达人签约申请审核通过
     * @param param
     * @return
     */
    @Override
    public ComusersignApplyThroughVo func_extcomacct_comusersign_verify(ComusersignApplyThroughForm param) {
        return ledT2RemoteService.callT2(EXTCOMACCT_COMUSERSIGN_VERIFY,param,ComusersignApplyThroughVo.class);
    }

    /**
     * 达人签约申请审核不通过
     * @param param
     * @return
     */
    @Override
    public ComusersignNotverifyVo func_extcomacct_comusersign_notverify(ComusersignNotverifyForm param) {
        return ledT2RemoteService.callT2(EXTCOMACCT_COMUSERSIGN_NOTVERIFY,param,ComusersignNotverifyVo.class);
    }

    /**
     * 产品代码黑名单查询
     * @param param
     * @return
     */
    @Override
    public StockBlackTabBackVo func_comtrade_comblackcode_qry(StockBlackTabForm param) {
        return ledT2RemoteService.callT2(COMTRADE_COMBLACKCODE_QRY,param,StockBlackTabBackVo.class);
    }

    /**
     * 组合申请审核通过
     * @param param
     * @return
     */
    @Override
    public CombineApplyVerifyVo func_combine_apply_verify(CombineApplyVerifyForm param) {
//        String senderComp_id=param.getSendercomp_id();
//        param.setSendercomp_id(senderComp_id);
        return ledT2RemoteService.callT2(COMBINE_APPLY_VERIFY,param,CombineApplyVerifyVo.class);
    }

    /**
     * 组合申请审核不通过
     * @param param
     * @return
     */
    @Override
    public CombineApplyNotverifyVo func_combine_apply_notverify(CombineApplyNotverifyForm param) {
        return ledT2RemoteService.callT2(COMBINE_APPLY_NOTVERIFY,param,CombineApplyNotverifyVo.class);
    }

    /**
     * 投顾组合申请查询
     * @param param
     * @return
     */
    @Override
    public CombineApplyQryBackVo func_combine_apply_qry(CombineApplyQryForm param) {
//        String companyId=param.getCompany_id();
//        param.setSendercomp_id(companyId);
        return ledT2RemoteService.callT2(COMBINE_APPLY_QRY,param,CombineApplyQryBackVo.class);
    }

    /**
     * 组合信息修改审核通过
     * @param param
     * @return
     */
    @Override
    public CombineModVerifyVo func_combine_mod_verify(CombineModVerifyForm param) {
        return ledT2RemoteService.callT2(COMBINE_MOD_VERIFY,param,CombineModVerifyVo.class);
    }

    /**
     * 组合信息修改审核不通过
     * @param param
     * @return
     */
    @Override
    public CombineModNotverifyVo func_combine_mod_notverify(CombineModNotverifyForm param) {
        return ledT2RemoteService.callT2(COMBINE_MOD_NOTVERIFY,param,CombineModNotverifyVo.class);
    }

    /**
     * 投顾组合信息修改申请查询
     * @param param
     * @return
     */
    @Override
    public CombineModapplyQryBackVo func_combine_modapply_qry(CombineModapplyQryForm param) {
        return ledT2RemoteService.callT2(COMBINE_MODAPPLY_QRY,param,CombineModapplyQryBackVo.class);
    }

    /**
     * 组合关闭申请审核通过
     * @param param
     * @return
     */
    @Override
    public CombineCloseapplyVo func_combine_closeapply_verify(CombineCloseapplyForm param) {
        return ledT2RemoteService.callT2(COMBINE_CLOSEAPPLY_VERIFY,param,CombineCloseapplyVo.class);
    }

    /**
     * 组合关闭申请审核不通过
     * @param param
     * @return
     */
    @Override
    public CombineCloseNotverifyVo func_combine_close_notverify(CombineCloseNotverifyForm param) {
        return ledT2RemoteService.callT2(COMBINE_CLOSE_NOTVERIFY,param,CombineCloseNotverifyVo.class);
    }

    /**
     * 投顾组合关闭申请查询
     * @param param
     * @return
     */
    @Override
    public CombCloseapplyQryBackVo func_combine_closeapply_qry(CombCloseapplyQryForm param) {
        String companyId=param.getCompany_id();
        param.setSendercomp_id(companyId);
        return ledT2RemoteService.callT2(COMBINE_CLOSEAPPLY_QRY,param,CombCloseapplyQryBackVo.class);
    }

    /**
     * 历史证券委托查询
     * @param param
     * @return
     */
    @Override
    public HiseCommQryBackVo func_his_securities_commission_qry(HiseCommQryForm param) {
        String passwordTypte=param.getPassword_type();
        if(passwordTypte.equals("2")) {//如果为交易密码
            String password=param.getPassword();
            //new_password解密
            byte[] res = RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(), util.Base64.decode(password));
            param.setPassword(new String(res));
        }
        return ledT2RemoteService.callT2(HIS_SECURITIES_COMMISSION_QRY,param,HiseCommQryBackVo.class);
    }

    public TradLoginBackVo func_dongbei_trans_auth_login(LoginAuthForm form){
        HeaderForm headerForm = new HeaderForm();
        headerForm.setCompany_id(form.getCompany_id());
        headerForm.setSendercomp_id(form.getSendercomp_id());
        form.setHeaderForm(headerForm);
        LoginAuthVo loginAuthVo= ledT2RemoteService.callT2(DONG_BEI_TRANS_LOGIN_AUTH,form,LoginAuthVo.class);
        TradLoginForm param = JSON.parseObject(JSON.toJSONString(form),TradLoginForm.class);
        param.setInput_content("1");
        param.setAccount_content(loginAuthVo.getFund_account());
        param.setContent_type(loginAuthVo.getContent_type());
        param.setPassword(loginAuthVo.getPassword());
        param.setPassword_type(loginAuthVo.getPassword_type());
        param.setHeaderForm(headerForm);
        TradLoginVo tradLogin= ledT2RemoteService.callT2(FUNC_ACCT_LOGIN,param,TradLoginVo.class);
        if(!"0".equals(tradLogin.getError_no())) {
            return VOUtil.genErrorResult(tradLogin.getError_no(),tradLogin.getError_info(),TradLoginBackVo.class);
        }

        //前往中台注册登录
        CapitalLoginForm capitalLoginForm = new CapitalLoginForm();
        capitalLoginForm.setLogin_account(MD5Util.encrypt(loginAuthVo.getFund_account()));
        capitalLoginForm.setAuth_password(MD5Util.encrypt("1111111111"));
        capitalLoginForm.setTenant_id(form.getCompany_id());
        capitalLoginForm.setCompany_id(form.getCompany_id());
        capitalLoginForm.setSendercomp_id(form.getSendercomp_id());
        capitalLoginForm.setHeaderForm(headerForm);
        UserLoginBack userLoginBack = ledT2RemoteService.callT2("847038",capitalLoginForm,UserLoginBack.class);
        if(!"0".equals(userLoginBack.getError_no())) {
            return VOUtil.genErrorResult(userLoginBack.getError_no(),userLoginBack.getError_info(),TradLoginBackVo.class);
        }
        BrokerVO brokerVO = new BrokerVO();
        UserTokenUtil.setBGBrokerVO(brokerVO, param);
        UserCache userCache = new UserCache();
        userCache.setBrokerVO(brokerVO);
        userCache.setAccount_content(param.getFund_account());
        userCache.setMiddle_user_token(userLoginBack.getUser_token());
        userCache.setIsCapitalAccount(userLoginBack.getIsCapitalAccount());
        userCache.setAccount_content(loginAuthVo.getFund_account());
        userCache.setPassword_type(loginAuthVo.getPassword_type());
        userCache.setPassword(loginAuthVo.getPassword());
        String user_token = UserTokenUtil.addUserToken(userCache);
        TradLoginBackVo tradLoginBackVo = new TradLoginBackVo();
        tradLoginBackVo.setUser_token(user_token);
        tradLoginBackVo.setMiddle_user_token(userLoginBack.getUser_token());
        return VOUtil.setSuccessResult(tradLoginBackVo);
    }

    public TradLoginBackVo func_dongbei_trans_login(TradLoginForm form){
        HeaderForm headerForm = new HeaderForm();
        headerForm.setCompany_id(form.getCompany_id());
        headerForm.setSendercomp_id(form.getSendercomp_id());
        form.setHeaderForm(headerForm);
        TradLoginVo tradLogin= ledT2RemoteService.callT2(FUNC_ACCT_LOGIN,form,TradLoginVo.class);
        if(!"0".equals(tradLogin.getError_no()) && !"370078".equals(tradLogin.getError_no())) {
            return VOUtil.genErrorResult(tradLogin.getError_no(),tradLogin.getError_info(),TradLoginBackVo.class);
        }
        //前往中台注册登录
        CapitalLoginForm capitalLoginForm = new CapitalLoginForm();
        capitalLoginForm.setLogin_account(MD5Util.encrypt(form.getAccount_content()));
        capitalLoginForm.setAuth_password(MD5Util.encrypt("1111111111"));
        capitalLoginForm.setTenant_id(form.getCompany_id());
        capitalLoginForm.setCompany_id(form.getCompany_id());
        capitalLoginForm.setSendercomp_id(form.getSendercomp_id());
        capitalLoginForm.setHeaderForm(headerForm);
        UserLoginBack userLoginBack = ledT2RemoteService.callT2("847038",capitalLoginForm,UserLoginBack.class);
        if(!"0".equals(userLoginBack.getError_no())) {
            return VOUtil.genErrorResult(userLoginBack.getError_no(),userLoginBack.getError_info(),TradLoginBackVo.class);
        }
        if( StringUtils.isBlank(userLoginBack.getUser_token())){
            return VOUtil.genErrorResult("1",userLoginBack.getError_info(),TradLoginBackVo.class);
        }

        if("370078".equals(tradLogin.getError_no())){
            form.setUser_id(userLoginBack.getUser_id()+"");
            tradLogin= ledT2RemoteService.callT2(FUNC_ACCT_LOGIN,form,TradLoginVo.class);
            if(!"0".equals(tradLogin.getError_no()) ) {
                return VOUtil.genErrorResult(tradLogin.getError_no(),tradLogin.getError_info(),TradLoginBackVo.class);
            }
        }
        BrokerVO brokerVO = new BrokerVO();
        UserTokenUtil.setBGBrokerVO(brokerVO, form);
        UserCache userCache = new UserCache();
        userCache.setBrokerVO(brokerVO);
        userCache.setAccount_content(form.getAccount_content());
        userCache.setMiddle_user_token(userLoginBack.getUser_token());
        userCache.setIsCapitalAccount(userLoginBack.getIsCapitalAccount());
        userCache.setAccount_content(form.getAccount_content());
        userCache.setPassword_type(form.getPassword_type());
        userCache.setPassword(form.getPassword());
        String user_token = UserTokenUtil.addUserToken(userCache);
        TradLoginBackVo tradLoginBackVo = new TradLoginBackVo();
        tradLoginBackVo.setUser_token(user_token);
        tradLoginBackVo.setMiddle_user_token(userLoginBack.getUser_token());
        return VOUtil.setSuccessResult(tradLoginBackVo);
    }



    @Override
    public DataDictionaryBackVO dataDictionaryQuery(DataDictionaryQueryForm param) {
        return ledT2RemoteService.callT2(DATA_DICTIONARY_QUERY, param, DataDictionaryBackVO.class);
    }

    @Override
    public ExtcomacctComuserLeaveVo extcomacct_comuser_leave(ExtcomacctComuserLeaveForm param) {
        return ledT2RemoteService.callT2(EXTCOMACCT_COMUSER_LEAVE, param, ExtcomacctComuserLeaveVo.class);
    }
}
